package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

//HTTPGet get 请求
func HTTPGet(uri string) ([]byte, error) {
	response, err := http.Get(uri)
	if err != nil {
		return nil, err
	}

	defer response.Body.Close()
	if response.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("http get error : uri=%v , statusCode=%v", uri, response.StatusCode)
	}
	return ioutil.ReadAll(response.Body)
}

func main() {

	//str, err := HTTPGet("http://www.baidu.com")
	//if err != nil {
	//	fmt.Println("err ", err)
	//}
	//fmt.Println(string(str))

	url := "http://127.0.0.1:9090/"
	// 表单数据
	contentType := "application/x-www-form-urlencoded"
	data := "name=枯藤&age=18"
	// json
	//contentType := "application/json"
	//data := `{"name":"枯藤","age":18}`
	resp, err := http.Post(url, contentType, strings.NewReader(data))
	if err != nil {
		fmt.Println("post failed, err:%v\n", err)
		return
	}
	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("get resp failed,err:%v\n", err)
		return
	}
	fmt.Println(string(b))

}
